package com.qinggeng.project.system.service.impl;

import com.qinggeng.framework.config.mq.MQSendUtils;
import com.qinggeng.framework.config.mq.dto.*;
import com.qinggeng.framework.config.mq.enums.MQTypeEnum;
import com.qinggeng.project.system.service.ISysConfigService;
import com.qinggeng.project.system.service.MQSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.qinggeng.project.system.service.impl.GiftServiceImpl.OUT_PAY_TIME_KEY;
import static com.qinggeng.project.system.service.impl.GiftServiceImpl.OUT_PRINT_TIME_KEY;
import static com.qinggeng.project.system.service.impl.OrdersServiceImpl.AFFIRM_OUT_TIME_KEY;
import static com.qinggeng.project.system.service.impl.OrdersServiceImpl.EVALUATE_OUT_TIME_KEY;

@Slf4j
@Service
public class MQSendServiceImpl implements MQSendService {

    public static final Long DAY=24L*60*60;

    public static final Long MINUTE=60L;

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private MQSendUtils mqSendUtils;


    @Override
    public void sendPrintStatusDelayMessage(String ordersNo){
        MessageOrdersDTO ordersDto =new MessageOrdersDTO();
        ordersDto.setType(MQTypeEnum.UPDATE_PRINT_STATUS.getType());
        ordersDto.setOrdersNo(ordersNo);
        //获取订单打印超时时间（单位：分钟）
        String outTime=sysConfigService.getConfigValue(OUT_PRINT_TIME_KEY);
        mqSendUtils.sendDelayMessage(ordersDto,Long.parseLong(outTime)*MINUTE);
    }

    @Override
    public void sendPayOutTimeDelayMessage(String ordersNo){
        log.info("发送订单支付超时消息"+ordersNo);
        MessageOrdersDTO ordersDto =new MessageOrdersDTO();
        ordersDto.setType(MQTypeEnum.PAY_OUT_TIME.getType());
        ordersDto.setOrdersNo(ordersNo);
        //获取订单支付超时时间（单位：分钟）
        String outTime=sysConfigService.getConfigValue(OUT_PAY_TIME_KEY);
        mqSendUtils.sendDelayMessage(ordersDto,Long.parseLong(outTime)*MINUTE);
    }

    @Override
    public void sendOrdersAutoTakeAsynMessage(String ordersNo){
        log.info("发送自动接单消息"+ordersNo);
        MessageOrdersDTO messageOrdersDTO =new MessageOrdersDTO();
        messageOrdersDTO.setType(MQTypeEnum.ORDERS_AUTO_TAKE.getType());
        messageOrdersDTO.setOrdersNo(ordersNo);
        mqSendUtils.sendAsynMessage(messageOrdersDTO);
    }

    @Override
    public void sendUserGoodsLimitAsynMessage(String ordersNo) {
        MessageOrdersDTO messageOrdersDTO=new MessageOrdersDTO();
        messageOrdersDTO.setType(MQTypeEnum.USER_GOODS_LIMIT_RECORD.getType());
        messageOrdersDTO.setOrdersNo(ordersNo);
        mqSendUtils.sendAsynMessage(messageOrdersDTO);
    }

    @Override
    public void sendOrdersAutoEvaluateDelayMessage(String ordersNo){
        MessageOrdersDTO messageOrdersDTO=new MessageOrdersDTO();
        messageOrdersDTO.setType(MQTypeEnum.ORDERS_AUTO_EVALUATE.getType());
        messageOrdersDTO.setOrdersNo(ordersNo);
        //获取自动评价超时时间（单位：天）
        String outTime=sysConfigService.getConfigValue(EVALUATE_OUT_TIME_KEY);
        //测试用
        //mqSendUtils.sendDelayMessage(messageOrdersDTO,Long.parseLong(outTime)*60);
        mqSendUtils.sendDelayMessage(messageOrdersDTO,Long.parseLong(outTime)*DAY);

    }

    @Override
    public void sendOrdersAutoAffirmDelayMessage(String ordersNo){
        MessageOrdersDTO messageOrdersDTO=new MessageOrdersDTO();
        messageOrdersDTO.setType(MQTypeEnum.ORDERS_AUTO_AFFIRM.getType());
        messageOrdersDTO.setOrdersNo(ordersNo);
        //获取自动评价超时时间（单位：天）
        String outTime=sysConfigService.getConfigValue(AFFIRM_OUT_TIME_KEY);
        //测试用
        //mqSendUtils.sendDelayMessage(messageOrdersDTO,Long.parseLong(outTime)*60);
        mqSendUtils.sendDelayMessage(messageOrdersDTO,Long.parseLong(outTime)*DAY);

    }

    @Override
    public void sendOrdersEndStatusUpdateAsynMessage(String ordersNo){
        MessageOrdersDTO messageOrdersDTO=new MessageOrdersDTO();
        messageOrdersDTO.setType(MQTypeEnum.ORDERS_END_STATUS_UPDATE.getType());
        messageOrdersDTO.setOrdersNo(ordersNo);
        mqSendUtils.sendAsynMessage(messageOrdersDTO);

    }

    @Override
    public void sendLuckyDrawAsynMessage(Long userId,String giftName){
        MessageLuckyDrawDTO messageLuckyDrawDTO = new MessageLuckyDrawDTO();
        messageLuckyDrawDTO.setType(MQTypeEnum.LUCKY_DRAW.getType());
        messageLuckyDrawDTO.setUserId(userId);
        messageLuckyDrawDTO.setGoodsName(giftName);
        mqSendUtils.sendAsynMessage(messageLuckyDrawDTO);
    }

   /* @Override
    public void sendOrdersAutoShipDelayMessage(String orderNo) {
        try {
            String config = (String) sysConfigService.selectConfigByKey("sys.autoShip.time");
            JSONObject jsonObject = JSONObject.parseObject(config);
            if (jsonObject == null){
                throw new CustomException("自动发货时间为空");
            }
            String shipTime = jsonObject.getString("shipTime");
            MessageOrdersAutoShipDTO messageOrdersAutoShipDTO = new MessageOrdersAutoShipDTO();
            messageOrdersAutoShipDTO.setOrdersNo(orderNo);
            messageOrdersAutoShipDTO.setType(MQTypeEnum.AUTO_SHIP.getType());
            mqSendUtils.sendDelayMessage(messageOrdersAutoShipDTO,Long.parseLong(shipTime));
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            e.printStackTrace();
        }
    }*/

    @Override
    public void sendPointSaleRecordAsynMessage(String ordersNo){
        log.info("发送商品统计消息{}"+ordersNo);
        MessageOrdersDTO messageOrdersDTO=new MessageOrdersDTO();
        messageOrdersDTO.setOrdersNo(ordersNo);
        messageOrdersDTO.setType(MQTypeEnum.POINT_SALE_RECORD.getType());
        mqSendUtils.sendAsynMessage(messageOrdersDTO);
    }

    @Override
    public void sendShareMoneyAsynMessage(Long userId, String money) {
        log.info("发送分享提现消息userId{} money{}",userId,money);
        MessageShareMoneyDTO d=new MessageShareMoneyDTO();
        d.setMoney(money);
        d.setUserId(userId);
        d.setType(MQTypeEnum.SHARE_ACCOUNT.getType());
        mqSendUtils.sendAsynMessage(d);
    }

    @Override
    public void sendGoodsPercentageDelayMessage(Long classifyId){
        log.info("发送商品提成消息{}",classifyId);
        MessageClassifyDTO classifyDTO=new MessageClassifyDTO();
        classifyDTO.setClassifyId(classifyId);
        classifyDTO.setType(MQTypeEnum.GOODS_PERCENTAGE_UPDATE.getType());
        mqSendUtils.sendDelayMessage(classifyDTO,6L);
    }

    @Override
    public void sendVipMessage(String orderNo) {
        log.info("发送vpi消息 orderNo {}",orderNo);
        MessageVipDTO d=new MessageVipDTO();
        d.setOrderNo(orderNo);
        d.setType(MQTypeEnum.VIP.getType());
        mqSendUtils.sendAsynMessage(d);
    }
}
